//
//  VTTableView.m
//  vTeam
//
//  Created by Zhang Hailong on 13-7-6.
//  Copyright (c) 2013年 hailong.org. All rights reserved.
//

#import "VTTableView.h"
#import "VTTableDataController.h"
#import <objc/runtime.h>

@interface VTTableView()
{
    Class _originalClass;
    id _vtTableViewDelegate;
}

@end

@implementation VTTableView

- (void)dealloc
{
    _vtTableViewDelegate = nil;
    _originalClass = nil;
    [super dealloc];
}

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        self.estimatedRowHeight = 0;
        self.estimatedSectionHeaderHeight = 0;
        self.estimatedSectionFooterHeight = 0;
    }
    return self;
}

- (id)initWithCoder:(NSCoder *)aDecoder
{
    self = [super initWithCoder:aDecoder];
    if (self) {
        // Initialization code
        self.estimatedRowHeight = 0;
        self.estimatedSectionHeaderHeight = 0;
        self.estimatedSectionFooterHeight = 0;
    }
    return self;
}

- (void)awakeFromNib
{
    [super awakeFromNib];
    self.estimatedRowHeight = 0;
    self.estimatedSectionHeaderHeight = 0;
    self.estimatedSectionFooterHeight = 0;
}
/*
 // Only override drawRect: if you perform custom drawing.
 // An empty implementation adversely affects performance during animation.
 - (void)drawRect:(CGRect)rect
 {
 // Drawing code
 }
 */

- (BOOL)vtIsDelegateValid
{
    Class currentClass = object_getClass(_vtTableViewDelegate);
    if (currentClass != _originalClass) {
        return NO;
    }
    return YES;
}

- (void)setDelegate:(id<UITableViewDelegate>)delegate
{
    _originalClass = object_getClass(delegate);
    _vtTableViewDelegate = delegate;
    [super setDelegate:delegate];
}

- (BOOL)canChangeContentOffset:(CGPoint)contentOffset
{
    if ([self.delegate isKindOfClass:[VTTableDataController class]]) {
        VTTableDataController * tableDataController = (VTTableDataController *)self.delegate;
        if ((tableDataController.loadedAnimating || tableDataController.topLoadingView.animating) &&
            self.contentOffset.y == (-tableDataController.topLoadingView.frame.size.height - self.contentInset.top)&&
            contentOffset.y == -self.contentInset.top) {
            return NO;
        }
    }
    
    return YES;
}

-(void) setContentOffset:(CGPoint)contentOffset{
    if (![self vtIsDelegateValid]) {
        [super setContentOffset:contentOffset];
        return;
    }
    
    if ([self canChangeContentOffset:contentOffset]) {
        [super setContentOffset:contentOffset];
        if(self.window){
            if([self.delegate respondsToSelector:@selector(tableView:didContentOffsetChanged:)]){
                [(id<VTTableViewDelegate>)self.delegate tableView:self didContentOffsetChanged:self.contentOffset];
            }
        }
    }
}

- (void)setContentSize:(CGSize)contentSize
{
    [super setContentSize:contentSize];
    if (![self vtIsDelegateValid]) {
        return;
    }
    if ([self.delegate respondsToSelector:@selector(tableView:didContentSizeChanged:)]) {
        [(id<VTTableViewDelegate>)self.delegate tableView:self didContentSizeChanged:contentSize];
    }
}

-(void) didMoveToWindow{
    [super didMoveToWindow];
    if([self.delegate respondsToSelector:@selector(tableView:didMoveToWindow:)]){
        [(id<VTTableViewDelegate>)self.delegate tableView:self didMoveToWindow:self.window];
    }
}

-(void) willMoveToWindow:(UIWindow *)newWindow{
    [super willMoveToWindow:newWindow];
    if([self.delegate respondsToSelector:@selector(tableView:willMoveToWindow:)]){
        [(id<VTTableViewDelegate>)self.delegate tableView:self willMoveToWindow:newWindow];
    }
}

@end
